package com.zllkj.mm.adapter

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.appcompat.widget.LinearLayoutCompat
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.tencent.mm.opensdk.utils.Log
import com.zllkj.mm.R
import com.zllkj.mm.common.ClientConfig
import com.zllkj.mm.module.shopping.fragment.OrderListItemFragment
import com.zllkj.mm.module.shopping.vm.ItemOrderListVM
import com.zllkj.mm.util.ImageUtils
import com.zllkj.mm.util.common.FormatUtil
import com.zllkj.mm.util.common.PictureLoadUtil
import kotlinx.android.synthetic.main.custom_item.view.*
import kotlinx.android.synthetic.main.fragment_goodslist.view.*
import java.math.BigDecimal

class OrderListAdapter(layoutResId: Int, data: List<ItemOrderListVM?>?) :
        BaseQuickAdapter<ItemOrderListVM, BaseViewHolder>(layoutResId, data) {
    override fun convert(baseViewHolder: BaseViewHolder, itemOrderListVM: ItemOrderListVM) {
        baseViewHolder.getView<AppCompatTextView>(R.id.tv_shop_name).text = itemOrderListVM.shopName
        baseViewHolder.getView<AppCompatTextView>(R.id.tv_goods_totalPrice).text = itemOrderListVM.totalPrices
        baseViewHolder.getView<AppCompatTextView>(R.id.tv_order_number).text = itemOrderListVM.orderGoodsNo
        var goodsArea = baseViewHolder.getView<LinearLayoutCompat>(R.id.ll_each_goods_area)
        goodsArea.removeAllViews()
        itemOrderListVM.goodsVoList.map {
            var eachGoodsItemView = LayoutInflater.from(mContext).inflate(R.layout.item_order_each_one_goods,null)
            eachGoodsItemView.findViewById<AppCompatTextView>(R.id.tv_each_one_goods_name).text = it.goodsName
            eachGoodsItemView.findViewById<AppCompatTextView>(R.id.tv_each_one_goods_count).text = it.buyCount.toString() + "件"
            eachGoodsItemView.findViewById<AppCompatTextView>(R.id.tv_each_one_goods_unitPrice).text = BigDecimal(it.unitPrice).divide(BigDecimal("100")).toEngineeringString()
            var image = eachGoodsItemView.findViewById<AppCompatImageView>(R.id.iv_each_one_goods_image)
            PictureLoadUtil.loadPic(image,it.goodsPic)
            goodsArea.addView(eachGoodsItemView)
        }



        var click1 = baseViewHolder.getView<AppCompatTextView>(R.id.tv_clicked_1)
        var click2 = baseViewHolder.getView<AppCompatTextView>(R.id.tv_clicked_2)
        var click3 = baseViewHolder.getView<AppCompatTextView>(R.id.tv_clicked_3)

        baseViewHolder.addOnClickListener(R.id.tv_clicked_1)
        baseViewHolder.addOnClickListener(R.id.tv_clicked_2)
        baseViewHolder.addOnClickListener(R.id.tv_clicked_3)

        var orderStatusTextView = baseViewHolder.getView<AppCompatTextView>(R.id.tv_order_status)

        Log.d("OrderListAdapter",itemOrderListVM.orderStage.toString())
        orderStatusTextView.text = FormatUtil.getNormalOrderStatus(itemOrderListVM.orderStage)
        //1-待付款、2-待发货、3-待收货、4-待评价、5-已完成、6-已取消、7-已关闭
        when(itemOrderListVM.orderStage){
            OrderListItemFragment.ORDER_WAIT_TO_PAY -> {
                //按钮显示取消订单 和 付款
                click1.visibility = View.GONE
                click2.visibility = View.VISIBLE
                click2.text = "取消订单"
                click2.background = mContext.getDrawable(R.drawable.strokegray_solid_gray_radius10)
                click2.setTextColor(Color.parseColor("#7B7B7B"))
                click3.visibility = View.VISIBLE
                click3.background = mContext.getDrawable(R.drawable.strokegray_solid_ff5421_radius10)
                click3.setTextColor(Color.parseColor("#FF5421"))
                click3.text = "付款"
            }
            OrderListItemFragment.ORDER_WAIT_TO_SHIPPING -> {
                //按钮显示提醒发货
                click1.visibility = View.GONE
                click2.visibility = View.GONE
                click3.visibility = View.VISIBLE
                click3.text = "提醒发货"
                click3.background = mContext.getDrawable(R.drawable.strokegray_solid_ff5421_radius10)
                click3.setTextColor(Color.parseColor("#FF5421"))
            }

            OrderListItemFragment.ORDER_WAIT_TO_RECEIVE -> {
                click1.visibility = View.GONE
                click2.visibility = View.GONE
                click3.visibility = View.VISIBLE
                click3.text = "确认收货"
                click3.background = mContext.getDrawable(R.drawable.strokegray_solid_ff5421_radius10)
                click3.setTextColor(Color.parseColor("#FF5421"))
            }
            OrderListItemFragment.ORDER_WAIT_TO_COMMONT -> {

                click1.visibility = View.GONE
                click2.visibility = View.GONE
                click3.text = "再次购买"
                click3.background = mContext.getDrawable(R.drawable.strokegray_solid_ff5421_radius10)
                click3.setTextColor(Color.parseColor("#FF5421"))
            }
            5 -> {

            }
            6 -> {
                baseViewHolder.getView<LinearLayoutCompat>(R.id.ll_status_button_show_of).visibility = View.GONE
            }
            7 -> {

            }
        }

    }


}